package ${packageOutPath}.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;



import com.cykj.common.api.users.vo.Users;
import com.cykj.common.cymall_utils.Constants;
import com.cykj.common.utils.BeanUtil;
import com.cykj.common.utils.QueryUtil;
import com.cykj.common.utils.VoUtil;
import com.cykj.cymall.service.impl.MemberService;
import com.cykj.cymall.vo.MemberVo;

import ${packageOutPath}.bean.${bean};
import ${packageOutPath}.dao.I${bean}Dao;
import ${packageOutPath}.service.I${bean}Service;
import ${packageOutPath}.vo.${bean}Vo;

@Service
public class ${bean}Service implements I${bean}Service {
		
	@Autowired
	private I${bean}Dao dao;
	@Autowired
	private MemberService memberService;
	
	

	@Override
	public ${bean}Vo getById(Integer id) {
		${bean} bean=dao.findOne(id);
		return (${bean}Vo)VoUtil.beanToVo(bean, new ${bean}Vo());
	}

	@Override
	public ${bean}Vo getByCondition(final ${bean}Vo vo) {
		Specification<${bean}> spec=new Specification<${bean}>() {
			@Override
			public Predicate toPredicate(Root<${bean}> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicate=null;
				try {
					predicate = QueryUtil.query(root, cb, vo);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				if(null!=predicate){
					Predicate[] pre = new Predicate[predicate.size()];
	                query.where(predicate.toArray(pre));
				}
				
				return null;
			}
		};
		${bean} bean=dao.findOne(spec);
		return (${bean}Vo)VoUtil.beanToVo(bean, vo);
	}

	@Override
	public List<${bean}Vo> queryAll(final Users user) {
		Specification<${bean}> spec=new Specification<${bean}>() {
			@Override
			public Predicate toPredicate(Root<${bean}> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("oid"), user.getOid()));
				return null;
			}
		};
		List<${bean}> beanList=dao.findAll(spec);
		List<${bean}Vo> voList=new ArrayList<${bean}Vo>();
		if(null!=beanList&&beanList.size()>0){
			for (${bean} ${bean} : beanList) {
				voList.add((${bean}Vo)VoUtil.beanToVo(${bean}, new ${bean}Vo()));
			}
		}
		return voList;
	}

	@Override
	public List<${bean}Vo> queryByCondition(final ${bean}Vo vo) {
		Specification<${bean}> spec=new Specification<${bean}>() {
			@Override
			public Predicate toPredicate(Root<${bean}> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicate=null;
				try {
					predicate = QueryUtil.query(root, cb, vo);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				if(null!=predicate){
					Predicate[] pre = new Predicate[predicate.size()];
	                query.where(predicate.toArray(pre));
				}
				return null;
			}
		};
		List<${bean}> beanList=dao.findAll(spec);
		List<${bean}Vo> voList=new ArrayList<${bean}Vo>();
		if(null!=beanList&&beanList.size()>0){
			for (${bean} ${bean} : beanList) {
				voList.add((${bean}Vo)VoUtil.beanToVo(${bean}, new ${bean}Vo()));
			}
		}
		return voList;
	}

	@Override
	public Map<String, Object> save(${bean}Vo vo,Users user) {
		Map<String,Object> res = new HashMap<String, Object>();
		String code = Constants.SUCCESS ;
		String msg =  Constants.SUCCESS_TXT ;
		
		${bean} bean=new ${bean}();
		bean=(${bean})VoUtil.voToBean(vo, bean);
		
		dao.save(bean);
		
		res.put("code", code);
		res.put("msg", msg);
		return res ;
	}

	@Override
	public Map<String, Object> update(${bean}Vo vo,Users user) {
		Map<String,Object> res = new HashMap<String, Object>();
		String code = Constants.SUCCESS ;
		String msg =  Constants.SUCCESS_TXT ;
		
		${bean} bean=new ${bean}();
		bean=(${bean})VoUtil.voToBean(vo, bean);
		
		${bean} dbBean=dao.findOne(vo.getSeqId());
		if(null!=dbBean){
			try {
				BeanUtil.copyBean(bean, dbBean);
			} catch (Exception e) {
				code=Constants.FAIL;
				msg=e.getLocalizedMessage();
			} 
		}else{
			
		}
		
		dao.save(dbBean);
		
		res.put("code", code);
		res.put("msg", msg);
		return res ;
	}



	@Override
	public Map<String, Object> deleteById(Integer id) {
		Map<String,Object> res = new HashMap<String, Object>();
		String code = Constants.SUCCESS ;
		String msg =  Constants.SUCCESS_TXT ;
		
		if(null!=id){
			dao.delete(id);
		}else{
			code=Constants.FAIL;
			msg="id为空";
		}
		
		
		
		res.put("code", code);
		res.put("msg", msg);
		return res ;
	}
	
	@Override
	public PageHelper<${bean}Vo> queryPage(Pageable pageable, final ${bean}Vo vo) {
		Specification<${bean}> spec = new Specification<${bean}>() {
			@Override
			public Predicate toPredicate(Root<${bean}> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//设置查询条件
				
				List<Predicate> predicate=null;
				try {
					predicate = QueryUtil.query(root, cb, vo);
					if(null!=predicate){
						Predicate[] pre = new Predicate[predicate.size()];
		                query.where(predicate.toArray(pre));
					}
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				
				
				//把查询条件加入查询语句
				return null;
			}
		};
		Page<${bean}> page= dao.findAll(spec,pageable);
		List<${bean}Vo> voList=new ArrayList<${bean}Vo>();
		for (${bean} person : page.getContent()) {
			voList.add((${bean}Vo)VoUtil.beanToVo(person, new ${bean}Vo()));
		}
		
		PageHelper<${bean}Vo> pageHelper=new PageHelper<${bean}Vo>();
		pageHelper.setPageNumber(page.getNumber()+1);
		pageHelper.setPageSize(page.getSize());
		pageHelper.setTotal(page.getTotalElements());
		pageHelper.setTotalPages(page.getTotalPages());
		pageHelper.setResults(voList);
		return pageHelper;
	}
	
	
}
